package com.example.list02;

import java.util.ArrayList;

import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ListView;
import android.widget.TextView;

public class MainActivity extends ActionBarActivity {
	ListView listView;

	String firstString = "title";
	ArrayList<String> firstGridDatas = new ArrayList<String>();
	ArrayList<String> secondGridDatas = new ArrayList<String>();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		makeMocks();
		listView = (ListView) findViewById(R.id.list);
		listView.setAdapter(new MyAdapter());

	}

	void makeMocks() {
		for (int i = 0; i < 10; i++) {
			firstGridDatas.add("first:" + i);
			secondGridDatas.add("second: " + i);
		}
	}

	class MyAdapter extends BaseAdapter {

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			View inflate = getLayoutInflater().inflate(R.layout.row_grid, null);
			if (position == 0) {
				inflate.findViewById(R.id.textView).setVisibility(View.VISIBLE);
				TextView tv = (TextView) inflate.findViewById(R.id.textView);
				tv.setText((String) getItem(position));
			} else if (position == 1) {
				GridView gridView = (GridView) inflate.findViewById(R.id.firstGridView);
				gridView.setVisibility(View.VISIBLE);
				ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1, firstGridDatas);
				gridView.setAdapter(arrayAdapter);
				arrayAdapter.notifyDataSetChanged();
			} else if (position == 2) {
				inflate.findViewById(R.id.secondGridView).setVisibility(View.VISIBLE);
			}
			return inflate;
		}

		@Override
		public int getCount() {
			return 3;
		}

		@Override
		public Object getItem(int position) {
			if (position == 0) {
				return firstString;
			} else if (position == 1) {
				return firstGridDatas;
			} else if (position == 2) {
				return secondGridDatas;
			} else {
				return null;
			}
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

	}

}
